package org.dromara.system.job;

import com.aizuda.snailjob.client.job.core.annotation.JobExecutor;
import com.aizuda.snailjob.client.job.core.dto.JobArgs;
import com.aizuda.snailjob.client.model.ExecuteResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.redis.common.RedisConstants;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.system.domain.SysSystem;
import org.dromara.system.domain.vo.DataBaseVo;
import org.dromara.system.service.ISysSystemService;
import org.dromara.system.utils.SystemUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 检测动环 渗漏水等5个服务的状态
 */
@JobExecutor(name = "CheckSystemStatusJob")
@Slf4j
@Component
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class CheckSystemStatusJob {

    private final ISysSystemService sysSystemService;

    public ExecuteResult jobExecute(JobArgs jobArgs) {
        // 获取系统状态信息
        DataBaseVo dataBaseVo = SystemUtils.getSystemInfo();
        // 构建系统状态列表
        List<SysSystem> systemStatusList = new ArrayList<>();
        systemStatusList.add(createSysSystem(1L, dataBaseVo.getModbusTcpStatusInfo()));
        systemStatusList.add(createSysSystem(2L, dataBaseVo.getHttpServerStatusInfo()));
        systemStatusList.add(createSysSystem(3L, dataBaseVo.getUdpServerStatusInfo()));
        systemStatusList.add(createSysSystem(4L, dataBaseVo.getBsmServerStatusInfo()));
        systemStatusList.add(createSysSystem(5L, dataBaseVo.getBsmServerStatusInfo()));

        // 批量更新数据库状态
        sysSystemService.updateBatchById(systemStatusList);

        return ExecuteResult.success("更新系统状态成功");
    }

    private SysSystem createSysSystem(Long id, Object statusInfo) {
        SysSystem sysSystem = new SysSystem();
        sysSystem.setId(id);

        if (statusInfo instanceof DataBaseVo.ModbusTcpStatusInfo modbusInfo) {
            sysSystem.setStatus(modbusInfo.getStatus());
            sysSystem.setName("综合管养");
            sysSystem.setUpdateTime(modbusInfo.getUpdateTime());
            sysSystem.setDescription(modbusInfo.getDesc());
        } else if (statusInfo instanceof DataBaseVo.HttpServerStatusInfo httpInfo) {
            sysSystem.setStatus(httpInfo.getStatus());
            sysSystem.setName("渗漏水");
            sysSystem.setUpdateTime(httpInfo.getUpdateTime());
            sysSystem.setDescription(httpInfo.getDesc());
        } else if (statusInfo instanceof DataBaseVo.UdpServerStatusInfo udpInfo) {
            sysSystem.setStatus(udpInfo.getStatus());
            sysSystem.setName("应急电话");
            sysSystem.setUpdateTime(udpInfo.getUpdateTime());
            sysSystem.setDescription(udpInfo.getDesc());
        } else if (statusInfo instanceof DataBaseVo.BsmServerStatusInfo bsmInfo) {
            sysSystem.setStatus(bsmInfo.getStatus());
            sysSystem.setName("动环监测");
            sysSystem.setUpdateTime(bsmInfo.getUpdateTime());
            sysSystem.setDescription(bsmInfo.getDesc());
        } else if (statusInfo instanceof DataBaseVo.YtServerStatusInfo bsmInfo) {
            sysSystem.setStatus(bsmInfo.getStatus());
            sysSystem.setName("应天大街");
            sysSystem.setUpdateTime(bsmInfo.getUpdateTime());
            sysSystem.setDescription(bsmInfo.getDesc());
        }

        return sysSystem;
    }




}
